package com.foreknow.service.impl;

import com.foreknow.dao.GuestbookDao;
import com.foreknow.dao.impl.GuestbookDaoimpl;
import com.foreknow.db.DBManager;
import com.foreknow.model.Guestbook;
import com.foreknow.service.GuestbookService;
import com.foreknow.util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class GuestbookServiceimpl implements GuestbookService {



    @Override
    public boolean save(Guestbook guestbook) {
        DBManager dbManager= DBManager.getInstance();
        // 连接数据库
      Connection conn= dbManager.getConnection();
        GuestbookDaoimpl guestbookDao=new GuestbookDaoimpl();
        //要将DBManger 中获取到的连接传递到BaseDAO中的setConnection
        guestbookDao.setConnection(conn);
        boolean isRight= false;
        try {
        isRight=   guestbookDao.addMessage(guestbook);
        //提交事物
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return isRight;
    }

    @Override
    public List<Object> queryinfo() {

        DBManager dbManager=DBManager.getInstance();
        Connection connection=dbManager.getConnection();
        GuestbookDaoimpl guestbookDao=new GuestbookDaoimpl();
          guestbookDao.setConnection(connection);
          List<Object> list=null;
        try {
         list= guestbookDao.getQueryAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public boolean deleteBean(Integer id) {
        DBManager dbManager= DBManager.getInstance();
        // 连接数据库
        Connection conn= dbManager.getConnection();
        GuestbookDaoimpl guestbookDao=new GuestbookDaoimpl();
        //要将DBManger 中获取到的连接传递到BaseDAO中的setConnection
        guestbookDao.setConnection(conn);
        boolean isRight= false;
        try {
            isRight=   guestbookDao.delectByid(id);
            //提交事物
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return isRight;
    }

    @Override
    public boolean isupdate(Guestbook guestbook) {
        DBManager dbManager= DBManager.getInstance();
        // 连接数据库
        Connection conn= dbManager.getConnection();
        GuestbookDaoimpl guestbookDao=new GuestbookDaoimpl();
        //要将DBManger 中获取到的连接传递到BaseDAO中的setConnection
        guestbookDao.setConnection(conn);
        boolean isRight=false;
        try {
          isRight=  guestbookDao.update(guestbook);
          conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return isRight;
    }

    @Override
    public Guestbook isgetid(int id) {
        DBManager dbManager= DBManager.getInstance();
        // 连接数据库
        Connection conn= dbManager.getConnection();
        GuestbookDaoimpl guestbookDao=new GuestbookDaoimpl();
        //要将DBManger 中获取到的连接传递到BaseDAO中的setConnection
        guestbookDao.setConnection(conn);
        Guestbook guestbook=  guestbookDao.getid(id);
            return guestbook;
    }

    @Override
    public Pagination getByPage(int pageSize, int PageNumber) {
        DBManager dbManager = DBManager.getInstance();
        //连接数据库
        Connection conn = dbManager.getConnection();
        GuestbookDaoimpl guestbookDao = new GuestbookDaoimpl();
        //将DBManager中获取到的连接传递给BaseDAO中的setConnection(conn)
        guestbookDao.setConnection(conn);
        Pagination pagination = null;
        try {
            pagination = guestbookDao.getGuestbookByPage(pageSize, PageNumber);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pagination;



    }

}
